package com.example.projectmanagement.interceptor;

import java.time.LocalDateTime;

public class ApiEndpointStatistics {
    private String url;
    private String method;
    private long requestCount = 0;
    private long errorCount = 0;
    private double totalResponseTime = 0;
    private double minResponseTime = Double.MAX_VALUE;
    private double maxResponseTime = 0;
    private LocalDateTime lastAccessTime;
    
    // 无参构造函数
    public ApiEndpointStatistics() {
    }
    
    // 带参数构造函数
    public ApiEndpointStatistics(String url, String method) {
        this.url = url;
        this.method = method;
    }
    
    // 增加请求计数
    public synchronized void incrementRequestCount() {
        this.requestCount++;
    }
    
    // 增加错误计数
    public synchronized void incrementErrorCount() {
        this.errorCount++;
    }
    
    // 更新响应时间统计
    public synchronized void updateResponseTime(long responseTime) {
        this.totalResponseTime += responseTime;
        if (responseTime < this.minResponseTime) {
            this.minResponseTime = responseTime;
        }
        if (responseTime > this.maxResponseTime) {
            this.maxResponseTime = responseTime;
        }
        this.lastAccessTime = LocalDateTime.now();
    }
    
    // 计算平均响应时间
    public double getAvgResponseTime() {
        return requestCount > 0 ? totalResponseTime / requestCount : 0;
    }
    
    // 计算成功率
    public double getSuccessRate() {
        return requestCount > 0 ? ((double)(requestCount - errorCount) / requestCount) * 100 : 0;
    }
    
    // Getters and Setters
    public String getUrl() {
        return url;
    }
    
    public void setUrl(String url) {
        this.url = url;
    }
    
    public String getMethod() {
        return method;
    }
    
    public void setMethod(String method) {
        this.method = method;
    }
    
    public long getRequestCount() {
        return requestCount;
    }
    
    public long getErrorCount() {
        return errorCount;
    }
    
    public double getTotalResponseTime() {
        return totalResponseTime;
    }
    
    public double getMinResponseTime() {
        return minResponseTime == Double.MAX_VALUE ? 0 : minResponseTime;
    }
    
    public double getMaxResponseTime() {
        return maxResponseTime;
    }
    
    public LocalDateTime getLastAccessTime() {
        return lastAccessTime;
    }
    
    public void setLastAccessTime(LocalDateTime lastAccessTime) {
        this.lastAccessTime = lastAccessTime;
    }
}